<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/8/31 10:00
 */

namespace App\Project\Reserve;

use App\User;

class Doctor
{
    /**
     * @var User
     */
    protected $doctor;

    public function create($name, $phone, $password, $hospital_id)
    {
        $account = time() . rand(100, 999);

        $data = compact('name', 'phone', 'password', 'hospital_id', 'account');

        $exist = User::where('phone', $phone)->count();

        if ($exist) {
            throw new \Exception('手机号已经被占用。');
        }

        $this->doctor = User::create($data);
    }

    public function update($input)
    {
        $this->doctor = User::simpleField()->findOrFail($input['id']);

        $result = $this->doctor->fill($input)->save();

        $this->doctor->detail->fill($input)->save();

        return $result;
    }

    public function setDoctorRole($role)
    {
        if ($role == 'doctor') {
            $this->doctor->type = 2;
        } elseif ($role == 'nurse') {
            $this->doctor->type = 3;
        } elseif ($role == 'admin') {
            $this->doctor->type = 0;
        }

        $this->doctor->assign($role);
        \Bouncer::refreshFor($this->doctor);
    }

    public function setDoctorJob($job)
    {
        if ($this->doctor instanceof User) {

            $this->doctor->detail->job = $job;

            return $this->doctor->detail->save();
        }

        return false;
    }

    /**
     * @return User
     */
    public function getDoctor()
    {
        return $this->doctor;
    }
}